package com.stormrage.edu.dao.learn.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.learn.LeaStudentDao;
import com.stormrage.entity.excel.StudentExcel;
import com.stormrage.entity.learn.LeaStudent;
import com.stormrage.entity.learn.rep.LeaStudentRep;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: LeaStudentDaoImpl.java
 * @Package com.stormrage.edu.dao.learn.impl
 * @Description: 学员 Dao
 * @author www.stormragetech.com
 * @date 2016年4月15日
 * @version V1.0
 */
@Repository
public class LeaStudentDaoImpl extends MysBaseDao implements LeaStudentDao {

	private static final Logger logger = LoggerFactory.getLogger(LeaStudentDaoImpl.class);

	@Override
	public Integer create(final LeaStudent arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 Create 】- 开始运行 ");

		final String query = "INSERT INTO lea_student (acct_id, bunch_id, apply_date, locked) VALUES"
				+ " (:acctId, :bunchId, :applyDate, :locked)";
		logger.debug(" 数据库执行语句为 " + query);

		Integer index = create(query, arg0);

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 Create 】- 完成运行 ");
		return index;

	}

	@Override
	public void update(final LeaStudent arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaBunchDao 】-【 Update 】- 开始运行 ");

		final String query = "UPDATE lea_student SET locked = :locked WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】-【 LeaBunchDao 】-【 Update 】- 完成运行 ");

	}

	@Override
	public Integer queryCountByBunchLock(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 QueryCountByBunchLock 】- 开始运行 ");

		final String query = "SELECT COUNT(*) FROM lea_student WHERE bunch_id = :bunchId AND locked = :locked";
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 QueryCountByBunchLock 】- 完成运行 ");
		return count;

	}

	@Override
	public Integer queryCountByBunch(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 QueryCountByBunchLock 】- 开始运行 ");

		final String query = "SELECT COUNT(*) FROM lea_student WHERE bunch_id = :bunchId" + map.get("query");
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 QueryCountByBunchLock 】- 完成运行 ");
		return count;

	}

	@Override
	public LeaStudent queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 QueryById 】- 开始运行 ");

		final String query = "SELECT * FROM lea_student WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		LeaStudent entity = queryObject(query, LeaStudent.class, map);

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 QueryById 】- 完成运行 ");
		return entity;

	}

	@Override
	public List<LeaStudentRep> queryListByBunch(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 QueryListByBunchLock 】- 开始运行 ");

		final String query = "SELECT a.id, a.locked, c.real_name, c.phone, c.email FROM lea_student AS a, stu_acct AS b,"
				+ " stu_info AS c WHERE a.acct_id = b.id AND b.id = c.acct_id AND a.bunch_id = :bunchId"
				+ map.get("query") + " LIMIT :startPage, :size";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaStudentRep> array = queryList(query, LeaStudentRep.class, map);

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 QueryListByBunchLock 】- 完成运行 ");
		return array;

	}

	@Override
	public List<LeaStudentRep> queryListByBunchLock(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 QueryListByBunchLock 】- 开始运行 ");

		final String query = "SELECT a.id, a.bunch_id, b.stu_no, c.real_name, d.term_id FROM lea_student AS a, stu_acct AS b,"
				+ " stu_info AS c, lea_bunch AS d WHERE a.acct_id = b.id AND b.id = c.acct_id AND a.bunch_id = d.id"
				+ " AND a.bunch_id = :bunchId AND a.locked = :locked " + map.get("query") + " LIMIT :startPage, :size";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaStudentRep> array = queryList(query, LeaStudentRep.class, map);

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 QueryListByBunchLock 】- 完成运行 ");
		return array;

	}

	@Override
	public List<StudentExcel> queryListByBunchForExcel(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 queryListByBunchForExcel 】- 开始运行 ");

		final String query = "SELECT b.acct, b.stu_no, c.real_name AS name, c.id_card AS idcard FROM lea_student AS a, stu_acct AS b,"
				+ " stu_info AS c WHERE a.acct_id = b.id AND b.id = c.acct_id AND a.bunch_id = :bunchId";
		logger.debug(" 数据库执行语句为 " + query);

		List<StudentExcel> array = queryList(query, StudentExcel.class, map);

		logger.debug(" 【 Dao 】-【 LeaStudentDao 】-【 queryListByBunchForExcel 】- 完成运行 ");
		return array;

	}

}
